package com.ruoyi.project.module.puser.controller;

import java.util.List;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.module.puser.domain.Puser;
import com.ruoyi.project.module.puser.service.IPuserService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;

/**
 * 自定义用户 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-11-06
 */
@Controller
@RequestMapping("/module/puser")
public class PuserController extends BaseController
{
    private String prefix = "module/puser";
	
	@Autowired
	private IPuserService puserService;
	
	@RequiresPermissions("module:puser:view")
	@GetMapping()
	public String puser()
	{
	    return prefix + "/puser";
	}
	
	/**
	 * 查询自定义用户列表
	 */
	/**
	 * 模糊查询
	 */
	@RequiresPermissions("module:puser:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Puser puser)
	{
		startPage();
        List<Puser> list = puserService.selectPuserList(puser);
		return getDataTable(list);
	}
	
	/**
	 * 新增自定义用户
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存自定义用户
	 */
	@RequiresPermissions("module:puser:add")
	@Log(title = "自定义用户", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Puser puser)
	{		
		return toAjax(puserService.insertPuser(puser));
	}

	/**
	 * 修改自定义用户
	 */
	@GetMapping("/edit/{userId}")
	public String edit(@PathVariable("userId") Integer userId, ModelMap mmap)
	{
		Puser puser = puserService.selectPuserById(userId);
		mmap.put("puser", puser);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存自定义用户
	 */
	@RequiresPermissions("module:puser:edit")
	@Log(title = "自定义用户", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Puser puser)
	{		
		return toAjax(puserService.updatePuser(puser));
	}
	
	/**
	 * 删除自定义用户
	 */
	@RequiresPermissions("module:puser:remove")
	@Log(title = "自定义用户", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(puserService.deletePuserByIds(ids));
	}

	@Log(title = "用户管理", businessType = BusinessType.EXPORT)
	@RequiresPermissions("module:puser:export")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(Puser puser) throws Exception
	{
		try
		{
			List<Puser> list =puserService.selectPuserList(puser);
			ExcelUtil<Puser> util = new ExcelUtil<Puser>(Puser.class);
			return util.exportExcel(list, "user");
		}
		catch (Exception e)
		{
			return error("导出Excel失败，请联系网站管理员！");
		}
	}



}
